package com.weixing.mall.provider.support.excalservice;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.EasyExcel;
import com.google.common.collect.Lists;
import com.weixing.mall.provider.model.domain.InDepotBillProduct;
import com.weixing.mall.provider.model.domain.OutDepotBillProduct;
import com.weixing.mall.provider.model.excel.DefectiveProductED;
import com.weixing.mall.provider.model.vo.DefectiveProductVo;
import com.weixing.mall.provider.model.vo.GoodsTransBillProductVo;
import com.weixing.mall.provider.model.vo.ProductStockListVo;
import com.weixing.mall.provider.support.excaldata.GoodsTransBillProductED;
import com.weixing.mall.provider.support.excaldata.InDepotProductED;
import com.weixing.mall.provider.support.excaldata.OutDepotProductED;
import com.weixing.mall.provider.support.excaldata.ProductStockListED;
import com.weixing.mall.util.SKUUtils;
import org.springframework.beans.BeanUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

/**
 * 仓库导出服务
 *
 */
public class DepotExcalService {

    /**
     * 仓库出库单详情
     * @param depotName
     * @param billSn
     * @param psvList
     * @param fileName
     * @param response
     */
    public static void OutDepotBillProductExcalDownload(String depotName,String billSn,List<OutDepotBillProduct> psvList,String fileName,HttpServletResponse response) {
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        try {
            if(StrUtil.isEmpty(fileName)){
                fileName = DateUtil.now();
            }
            fileName = URLEncoder.encode(fileName, "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), OutDepotProductED.class).sheet("sheet1").doWrite(loadOutDepotProductEDList(depotName, billSn, psvList));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 装载出库管理导出数据
     */
    public static List<OutDepotProductED> loadOutDepotProductEDList(String depotName,String billSn,List<OutDepotBillProduct> psvList){
        List<OutDepotProductED> list = Lists.newArrayList();
        psvList.forEach(psv -> {
            OutDepotProductED odp = new OutDepotProductED();
            odp.setDepotName(depotName);
            odp.setBillSn(billSn);
            odp.setGoodsName(psv.getGoodsName());
            odp.setStyleNo(psv.getStyleNo());
            odp.setQuantity(psv.getQuantity());
            odp.setSku(psv.getSpec());
            if(ObjectUtil.isNotEmpty(psv.getSpec())){
                odp.setColor(SKUUtils.color2(psv.getSpec()));
                odp.setSize(SKUUtils.size2(psv.getSpec()));
            }
            odp.setProductSn(psv.getProductSn());
            odp.setImg(psv.getProductImg());
            odp.setStorehouse(psv.getStorehouse());
            list.add(odp);
        });
        return list;
    }

    /**
     * 调货出库单详情导出
     * @param billSn
     * @param outdepotName
     * @param indepotName
     * @param gtbpvList
     * @param fileName
     * @param response
     */
    public static void GoodsTransBillProductExcalDownload(String billSn,String outdepotName,String indepotName,  List<GoodsTransBillProductVo> gtbpvList, String fileName, HttpServletResponse response) {
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        try {
            if(StrUtil.isEmpty(fileName)){
                fileName = DateUtil.now();
            }
            fileName = URLEncoder.encode(fileName, "UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), GoodsTransBillProductED.class).sheet("sheet1").doWrite(loadGoodsTransBillProductEDList(billSn, outdepotName, indepotName, gtbpvList));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 装载出库管理导出数据
     */
    public static List<GoodsTransBillProductED> loadGoodsTransBillProductEDList(String billSn,String outdepotName,String indepotName,List<GoodsTransBillProductVo> gtbpvList){
        List<GoodsTransBillProductED> list = Lists.newArrayList();
        gtbpvList.forEach(gtbpv -> {
            GoodsTransBillProductED gtbp = new GoodsTransBillProductED();
            gtbp.setBillSn(billSn);
            gtbp.setOutdepotName(outdepotName);
            gtbp.setIndepotName(indepotName);
            gtbp.setGoodsName(gtbpv.getGoodsName());
            gtbp.setProductSn(gtbpv.getProductSn());
            gtbp.setQuantity(gtbpv.getQuantity());
            gtbp.setSku(gtbpv.getSpec());
            gtbp.setStock(gtbpv.getProductStock());
            gtbp.setStyleNo(gtbpv.getStyleNo());
            gtbp.setImg(gtbpv.getProductImg());
            gtbp.setStockSn(gtbpv.getStockSn());
            gtbp.setStorehouse(gtbpv.getStorehouse());
            if(ObjectUtil.isNotEmpty(gtbpv.getSpec())){
                gtbp.setColor(SKUUtils.color2(gtbpv.getSpec()));
                gtbp.setSize(SKUUtils.size2(gtbpv.getSpec()));
            }
            list.add(gtbp);
        });
        return list;
    }

    /**
     * 仓库列表数据导出服务
     */
    public static List<ProductStockListED> loadProductStockListList(List<ProductStockListVo> listVos){
        List<ProductStockListED> listEDS = Lists.newArrayList();
        listVos.forEach(psv -> {
            ProductStockListED ps = new ProductStockListED();
            BeanUtils.copyProperties(psv,ps);
            ps.setDepotSnStr(psv.getStockSn());
            if(ObjectUtil.isNotEmpty(psv.getProductSkuDesp())){
                ps.setColor(SKUUtils.color2(psv.getProductSkuDesp()));
                ps.setSize(SKUUtils.size2(psv.getProductSkuDesp()));
            }
            listEDS.add(ps);
        });
        return listEDS;
    }

    /**
     * 仓库入库单详情
     * @param depotName
     * @param billSn
     * @param psvList
     * @param fileName
     * @param response
     */
    public static void InDepotBillProductExcalDownload(String depotName, String billSn, List<InDepotBillProduct> psvList, String fileName, HttpServletResponse response) {
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        try {
            if(StrUtil.isEmpty(fileName)){
                fileName = DateUtil.now();
            }
            fileName = URLEncoder.encode(fileName, "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), InDepotProductED.class).sheet("sheet1").doWrite(loadInDepotProductEDList(depotName, billSn, psvList));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 装载入库管理导出数据
     */
    public static List<InDepotProductED> loadInDepotProductEDList(String depotName, String billSn, List<InDepotBillProduct> psvList){
        List<InDepotProductED> list = Lists.newArrayList();
        psvList.forEach(psv -> {
            InDepotProductED odp = new InDepotProductED();
            odp.setDepotName(depotName);
            odp.setBillSn(billSn);
            odp.setGoodsName(psv.getGoodsName());
            odp.setStyleNo(psv.getStyleNo());
            odp.setQuantity(psv.getQuantity());
            odp.setSku(psv.getSpec());
            odp.setProductSn(psv.getProductSn());
            odp.setImg(psv.getProductImg());
            odp.setStorehouse(psv.getStorehouse());
            if(ObjectUtil.isNotEmpty(psv.getSpec())){
                odp.setColor(SKUUtils.color2(psv.getSpec()));
                odp.setSize(SKUUtils.size2(psv.getSpec()));
            }
            list.add(odp);
        });
        return list;
    }

    /**
     * 次品库列表数据导出服务
     */
    public static List<DefectiveProductED> loadDefectiveProductList(List<DefectiveProductVo> listVos){
        List<DefectiveProductED> listEDS = Lists.newArrayList();
        listVos.forEach(dp -> {
            DefectiveProductED dpd = new DefectiveProductED();
            BeanUtils.copyProperties(dp,dpd);
            if(ObjectUtil.isNotEmpty(dp.getProductSkuDesp())){
                dpd.setColor(SKUUtils.color2(dp.getProductSkuDesp()));
                dpd.setSize(SKUUtils.size2(dp.getProductSkuDesp()));
            }
            listEDS.add(dpd);
        });
        return listEDS;
    }
}
